package com.woniu.service.job;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.mapper.CarMapper;
import com.woniu.mapper.OrderMapper;
import com.woniu.mapper.UserMapper;
import com.woniu.pojo.Car;
import com.woniu.pojo.Order;
import com.woniu.pojo.User;
import com.woniu.utils.SendPaySmsUtil;
import io.swagger.annotations.Example;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.Scheduled;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

//@Configuration
public class OrderTimeoutCheckJob {

    @Resource
    private OrderMapper orderMapper;

    @Resource
    private CarMapper carMapper;

    @Resource
    private UserMapper userMapper;

    @Scheduled(cron = "0/10 * * * * ?")
    public void checkOrder() {
        //1.查询超过30分钟，订单状态依然为待支付状态的订单
        QueryWrapper<Order> wrapper = new QueryWrapper<>();
        wrapper.eq("status", "1");
        Date time = new Date(System.currentTimeMillis() - 30 * 60 * 1000);
        wrapper.lt("leave_time", time);

        List<Order> orders = orderMapper.selectList(wrapper);
        Date update = new Date(System.currentTimeMillis());
        //  发送短信
        SendPaySmsUtil paySmsUtil = new SendPaySmsUtil();
        for (int i = 0; i < orders.size(); i++) {
            // 更新updateTime
            orders.get(i).setUpdateTime(update);
            orderMapper.updateById(orders.get(i));
            // 查询电话号
            Long carId = orders.get(i).getCarId();
            Car car = carMapper.selectById(carId);
            Long userId = car.getUserId();
            User user = userMapper.selectById(userId);
            String phone = "";
            if(user != null){
                phone = user.getPhone();
            }

            // TODO 短信接口审核没下来 发送短信
            // paySmsUtil.sendSms(phone, orders.get(i).getLeaveTime(), orders.get(i).getMoney());
            System.out.println(orders.get(i).getId() + "号订单未支付，向" + phone + "发送短信=================");
        }
    }
}
